************************************************************
****************** REPLICATION CODE ************************
************************************************************
* For: Rigterink, The Wane of Command, APSR				   *
* Simulation of attribution bias						   *
************************************************************
* Content of file:										   *
/*														   *
1. Preliminaries and setting directory					   *
2. Retrieving unattributed attacks from GTD				   *
3. Merging with main dataset							   *
4. Effect of drone strikes on unattributed attacks		   *
5. Simulation of attribution bias						   *
6. Table with results from simulation					  */
************************************************************

/*----------------------------------------------------------
---------- Preliminaries and setting directory -------------
----------------------------------------------------------*/

clear all
version 14
set more off

*******************************
*** Set your directory here ***
*******************************
cd "[directory]"

cap log close
log using log\Rigterink_drones_replication_appendix_attributionbias.log, replace


/*----------------------------------------------------------
-------- Retrieving unattributed attacks from GTD ----------
----------------------------------------------------------*/

* Dataset with total attributed and unattributed attacks per period
use dta/Rigterink_drones_replication_GTD2016unknown.dta, clear

* Labelling periodid
forvalues i = 1(1)144 {
	local label = floor((`i'-1)/12+2004)
	label define periods `i' "`label'", modify
}

label val periodid periods

******************
*** Figure B.6 ***
******************

graph twoway (line groupunknown periodid, lcolor(black)) ///
(line groupknown periodid, lcolor(gray)), ///
xlabel(1(12)133, valuelabel) xtitle(Time) ///
ytitle(Number of terrorist attacks) ///
legend(order(1 "Unattributed" 2 "Attributed")) /// 
plotregion(style(none)) graphregion(ifcolor(white) fcolor(white) color(white) icolor(white))

graph export graphs/Rigterink_AfigB6.pdf, replace


/*----------------------------------------------------------
--------------- Merging with main dataset ------------------
----------------------------------------------------------*/

use dta/Rigterink_drones_replication_GTD2016unknown.dta, clear

* Mering with main analysis dataset
merge 1:m periodid using dta/Rigterink_drones_replication_maindataset.dta, keepusing(bigfishtarget bigfishdied terratt terratt_unlog dronestrike groupid) nogen

xtset groupid periodid 

* Generating rolling average of unattributed attacks 
gen share = (L1.terratt_unlog+terratt_unlog+F1.terratt_unlog) / ///
(L1.groupknown+groupknown+F1.groupknown) 

label var share "3-month rolling average of unattributed attacks"


/*----------------------------------------------------------
---- Effect of drone strikes on unattributed attacks -------
----------------------------------------------------------*/

preserve

collapse (max) bigfishdied bigfishtarget groupunknown (rawsum) dronestrike, by(periodid)
replace groupunknown = ln(groupunknown+1)

tsset periodid

newey groupunknown L(0/6).bigfishdied F(1/6).bigfishdied  L(0/6).bigfishtarget F(1/6).bigfishtarget L(0/6).dronestrike F(1/6).dronestrike periodid, lag(12)
estimates store groupunknown

* p-values F-test for graphs
testparm L1.bigfishdied L2.bigfishdied L3.bigfishdied L4.bigfishdied L5.bigfishdied L6.bigfishdied
local lagF = round(r(p), 0.0001)
testparm F1.bigfishdied F2.bigfishdied F3.bigfishdied F4.bigfishdied F5.bigfishdied F6.bigfishdied 
local leadF = round(r(p), 0.0001)


* Results graph
coefplot /// 
(groupunknown,  mcolor(black) keep(*bigfishdied bigfishdied) ciopts(lcolor(black) recast(rcap))), ///
vert order(F9.* F8.* F7.* F6.* F5.* F4.* F3.* F2.* F1.* bigfish* L1.* L2.* L3.* L4.* L5.* L6.* L7.* L8.* L9.*) ///
yline(0, lcolor(black)) xline(7, lcolor(red)) nooffsets ///
plotregion(style(none)) graphregion(ifcolor(white) fcolor(white) color(white) icolor(white)) ///
ytitle("Coefficient estimate on hit") title("Unattributed terrorist attacks") ///
yscale(range(-0.5 1)) ytick(-0.5(0.5)1) ylabel(-0.5(0.5)1) ///
legend(off) ///
groups(L4.bigfishdied = "Prob > F lags: `: di %9.4f `lagF''" F4.bigfishdied = "Prob > F leads: `: di %9.4f `leadF''", nogap) ///
xtick(1(1)13) xlabel(1 "t-6" 2 "t-5" 3 "t-4" 4 "t-3" 5 "t-2" 6 "t-1" 7 "t" 8 "t+1" 9 "t+2" 10 "t+3" 11 "t+4" 12 "t+5" 13 "t+6") 

graph export graphs/Rigterink_AFigB7.pdf, replace

restore


/*----------------------------------------------------------
------------- Simulation of attribution bias ---------------
----------------------------------------------------------*/

set more off
set matsize 1000

forvalues R = 1(1)1000 {
	
	di "Round `R'"
	
	* Setting seed
	local seed = 19860223+`R'
	set seed `seed'
	
	* Generating number of unattributed attacks based on rolling share
	qui gen unattributed = rbinomial(groupunknown, share)
	qui replace unattributed = 0 if share==0
	qui gen simdep = ln(terratt_unlog+unattributed+1)
	
	* Regression
	qui reghdfe simdep L(0/6).bigfishdied F(1/6).bigfishdied  L(0/6).bigfishtarget F(1/6).bigfishtarget L(0/6).dronestrike F(1/6).dronestrike, absorb(i.groupid i.periodid) vce(, bw(12))

	matrix simulated = r(table)
		
	* Determining if any of the coefficients are significant at the 5% level
	forvalues j = 2(1)7 {
		local coef`j' = simulated[1,`j']
		local pval`j' = simulated[4,`j']
	}

	if (`coef2'>0 & `pval2'<0.05) | /// 
	(`coef3'>0 & `pval3'<0.05) | ///
	(`coef4'>0 & `pval4'<0.05) | ///
	(`coef5'>0 & `pval5'<0.05) | ///
	(`coef6'>0 & `pval6'<0.05) | ///
	(`coef7'>0 & `pval7'<0.05) {
	local significant = 1
	}
	else {
		local significant = 0
	}
	
	* F-statistic
	qui testparm L1.bigfishdied L2.bigfishdied L3.bigfishdied L4.bigfishdied L5.bigfishdied L6.bigfishdied
	local simpval = r(p)

	* Storing result in matrix
	matrix result = (`R', `significant', `simpval', `coef2', `coef3', `coef4', `coef5', `coef6', `coef7', `pval2', `pval3', `pval4', `pval5', `pval6', `pval7')
					
	if `R'==1 {
		matrix allresults = result
	}
	else {
		matrix allresults = (allresults \ result)
	}
				
	drop simdep unattributed
	
}


* Converting to dataset

clear
svmat allresults

rename (allresults1 allresults2 allresults3 allresults4 allresults5 allresults6 allresults7 allresults8 allresults9 allresults10 allresults11 allresults12 allresults13 allresults14 allresults15) (simulationid significant pval coef1 coef2 coef3 coef4 coef5 coef6 pval1 pval2 pval3 pval4 pval5 pval6)

save dta/Rigterink_drones_replication_attributionbias.dta, replace


/*----------------------------------------------------------
----------- Table with results from simulation -------------
----------------------------------------------------------*/
	
use dta/Rigterink_drones_replication_attributionbias.dta, clear

* Share of models with at least one coefficient significant at 5% level
sum significant

* Generating implied 95% confidence interval
for var coef?: gen p5_X = X \ rename X p95_X

collapse (mean) significant (p2) p5_coef? (p98) p95_coef?
drop significant

gen id=_n
reshape long p5_coef p95_coef, i(id) j(time)
drop id

* Some labelling
label var p5_coef "Lower bound implied 96\% CI"
label var p95_coef "Upper bound implied 96\% CI"
label var time "Lag of hit"
label define time 1 "t+1" 2 "t+2" 3 "t+3" 4 "t+4" 5 "t+5" 6 "t+6"
label val time time

* Outputting table
texsave using "tables/Rigterink_ATabB2.tex", replace nofix varlab frag title("Simulation of bias in allocating attacks by unknown perpetrator") location(h) marker("Atab:allocationbias") footnote("This table displays 2th and 98th percentile of simulated coefficients obtained when allocating terrorist attacks with an unknown perpetrator to terrorist groups included in this study based on the 3-month rolling share of worldwide terrorist attacks with a known perpetrator that these groups committed")

*******************
*** END OF FILE ***
*******************
